//
//  CaseDetailsViewController.m
//  UI_HLJ_Frame
//
//  Created by dllo on 15/7/7.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "CaseDetailsViewController.h"

#import "UIImageView+WebCache.h"
#import "HTTPTool.h"
#import "AFNetworking.h"
#import "weddingThingModel.h"

// model
#import "CaseDetaileModel.h"
// 文字和图片自适应
#import "CaseDetaileTableViewCell.h"
#import "CaseTextDetaileTableViewCell.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface CaseDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIView *headerView;
@property(nonatomic, assign)CGFloat H;
@property(nonatomic, retain)NSIndexPath *indexPath;

// 最上边的图的字典
@property(nonatomic, retain)NSDictionary *titlePicDic;
@property(nonatomic, retain)NSDictionary *titleDic;
// tableView上的图
@property(nonatomic, retain)NSMutableArray *picArr;
// headerView上的图
@property(nonatomic, retain)UIImageView *titleImageView;

// title图上的两个label
@property(nonatomic, retain)UILabel *upLabel;
@property(nonatomic, retain)UILabel *downLabel;

// 毛玻璃
@property(nonatomic, retain)UIVisualEffectView *effectView;

@end

@implementation CaseDetailsViewController

- (void)dealloc
{
    [_caseIdStr release];
    [_tableView release];
    [_headerView release];
    [_indexPath release];
    [_titleDic release];
    [_titleImageView release];
    [_titlePicDic release];
    [_picArr release];
    [_upLabel release];
    [_downLabel release];
    [_effectView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSMutableArray array];

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    
    
    
//    [self.tableView addSubview:self.headerView];
    [self creatData];
    [self creatTableView];
    
    

    
}
- (void)creatTableView
{
    // 导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
    
    self.title = @"💎 案 例 💎";
    
    UIBarButtonItem *bar=[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem=bar;
    
    self.tableView = [[UITableView alloc ]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    //不显示cell间分割线
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    self.headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 3)];
    self.headerView.backgroundColor = [UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.000];
    self.tableView.tableHeaderView = self.headerView;
    
    self.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 3)];
    [self.headerView addSubview:self.titleImageView];
    [self.titleImageView release];
//    self.titleImageView.backgroundColor = [UIColor redColor];
    
    self.upLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, HEIGHT / 4, WIDTH - 20, 20)];
//    self.upLabel.backgroundColor = [UIColor redColor];
    [self.tableView addSubview:self.upLabel];
    self.upLabel.font = [UIFont systemFontOfSize:14];
    [self.upLabel release];
    
    self.downLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, HEIGHT / 4+20, WIDTH - 20, 20)];
//    self.downLabel.backgroundColor = [UIColor redColor];
    [self.tableView addSubview:self.downLabel];
    self.downLabel.font = [UIFont systemFontOfSize:18];
    [self.downLabel release];
    
#pragma mark 毛玻璃
    UIBlurEffect *efffect = [UIBlurEffect effectWithStyle:0];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:efffect];
    self.effectView.frame = CGRectMake(0, 0, self.titleImageView.frame.size.width, self.titleImageView.frame.size.height + 2);
    self.effectView.alpha = 0.6;
    [self.titleImageView addSubview:self.effectView];
    [self.effectView release];
    
}
// 毛玻璃尺寸
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y + 20 < 0) {
//        self.titleImageView.frame = CGRectMake( -((HEIGHT / 3 - scrollView.contentOffset.y - 20) * 3/ HEIGHT * WIDTH - WIDTH) /2, 0, (HEIGHT / 3 - scrollView.contentOffset.y - 20) * WIDTH * 3/ HEIGHT , HEIGHT / 3 - scrollView.contentOffset.y - 20);
//        self.effectView.frame = CGRectMake(0, 0, self.titleImageView.frame.size.width, self.titleImageView.frame.size.height + 2);
//        self.effectView.alpha = 0.6 + (scrollView.contentOffset.y + 20) / 70;
//    }else{
//        self.titleImageView.frame = CGRectMake(0, - 20 -scrollView.contentOffset.y, WIDTH, HEIGHT / 3);
//        self.effectView.frame = CGRectMake(0, 0, self.titleImageView.frame.size.width, self.titleImageView.frame.size.height + 2);
//    }
//}

- (void)creatData
{
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *url = [NSString stringWithFormat:@"http://hunliji.com/v2/api/app/works/%@.json", self.caseIdStr];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.titlePicDic = dic[@"work"];
        
//        NSArray *titlePicText = self.titlePicDic[@"merchant"];
        self.titleDic = self.titlePicDic[@"merchant"];
        
        NSArray *arr = self.titlePicDic[@"media_items"];
        for (NSDictionary *tempDic in arr) {
             CaseDetaileModel *caseModel= [[CaseDetaileModel alloc]init];
            [caseModel setValuesForKeysWithDictionary:tempDic];

        [self.picArr addObject:caseModel];
            
            }
        
        [self.tableView reloadData];
//        NSLog(@"详情数据");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     
        }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if ([self.titlePicDic[@"describe"] isKindOfClass:[NSNull class]]) {
            return 0;
        }else{
            return 1;
        }
    }else if(section == 1)
    {
        return self.picArr.count;
    }else{
        return 0;
    }
//    return self.picArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSInteger section = indexPath.section;
    if (indexPath.section == 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
        // 根据文本计算高度,限制一个文本的最大范围(375,0)
        // 第二个属性作用:指定一个属性来定义对应文本和字体所占用的高度
        CGRect rect = [self.titlePicDic[@"describe"] boundingRectWithSize:CGSizeMake(355, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 50;
    }else if(indexPath.section == 1){
    
        CaseDetaileModel *caseDetaileModel = self.picArr[indexPath.row];
        CGFloat a = [caseDetaileModel.width floatValue];
        CGFloat b = [caseDetaileModel.height floatValue];
        if (a == 0) {
            return 0;
        }else{
            self.H = (self.view.frame.size.width*b/a);
            return self.H;
        }
    }else{
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//    NSInteger section = indexPath.section;
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        CaseTextDetaileTableViewCell *caseTextDetaileTableViewCell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!caseTextDetaileTableViewCell) {
            caseTextDetaileTableViewCell = [[CaseTextDetaileTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            caseTextDetaileTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        caseTextDetaileTableViewCell.details.text = self.titlePicDic[@"describe"];
        caseTextDetaileTableViewCell.details.backgroundColor = [UIColor yellowColor];

        NSString *str1 = self.titleDic[@"name"];
        self.upLabel.text = str1;
        NSString *str2 = self.titlePicDic[@"title"];
        self.downLabel.text = str2;

        
        return caseTextDetaileTableViewCell;
//    }else if (indexPath.section == 1){
//    
//        static NSString *reuse1 = @"reuse1";
//        CaseDetaileTableViewCell *caseDetaileTableViewCell = [tableView dequeueReusableCellWithIdentifier:reuse1];
//        if (!caseDetaileTableViewCell) {
//            caseDetaileTableViewCell = [[CaseDetaileTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1];
//            caseDetaileTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//        CaseDetaileModel *caseDetaileModel = self.picArr[indexPath.row];
//        //    caseDetaileModel.media_path = self.titlePicDic[@"media_items"];
//        [caseDetaileTableViewCell.picImageView sd_setImageWithURL:[NSURL URLWithString:caseDetaileModel.media_path] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
//            NSString *str = self.titlePicDic[@"cover_path"];
//            [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:str]];
//            [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"占位图"]];
            
//            if ([self.titlePicDic[@"cover_path"] hasSuffix:@"mp4"]) {
//                [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:@"mao"]];
//            }
   

//        }];
        
//        return caseDetaileTableViewCell;
    }else{
        return nil;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
